using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HuaRongDaoGame.Services;
using HuaRongDaoGame.Models;

namespace HuaRongDaoGame.Forms
{
    /// <summary>
    /// 存档选择窗体
    /// </summary>
    public partial class SaveSelectionForm : Form
    {
        public string SelectedSave { get; private set; }

        public SaveSelectionForm()
        {
            InitializeForm();
        }

        /// <summary>
        /// 初始化窗体
        /// </summary>
        private void InitializeForm()
        {
            this.Text = "选择存档";
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Size = new Size(600, 400);
            this.BackColor = Color.LightGray;

            CreateControls();
        }

        /// <summary>
        /// 创建控件
        /// </summary>
        private void CreateControls()
        {
            // 标题标签
            var titleLabel = new Label
            {
                Text = "请选择要加载的存档",
                Font = new Font("微软雅黑", 16, FontStyle.Bold),
                ForeColor = Color.DarkBlue,
                TextAlign = ContentAlignment.MiddleCenter,
                Size = new Size(500, 30),
                Location = new Point(50, 20)
            };

            // 存档列表视图
            var saveListView = new ListView
            {
                Font = new Font("微软雅黑", 10),
                Size = new Size(500, 250),
                Location = new Point(50, 60),
                View = View.Details,
                FullRowSelect = true,
                GridLines = true
            };

            // 添加列
            saveListView.Columns.Add("存档名称", 150);
            saveListView.Columns.Add("保存时间", 150);
            saveListView.Columns.Add("布局", 100);
            saveListView.Columns.Add("步数", 50);
            saveListView.Columns.Add("状态", 50);

            // 获取所有存档
            var saves = GameDataService.GetAllSaves();
            foreach (var save in saves)
            {
                var item = new ListViewItem(save.SaveName);
                item.SubItems.Add(save.SaveTime.ToString("yyyy-MM-dd HH:mm"));
                item.SubItems.Add(save.LayoutName);
                item.SubItems.Add(save.MoveCount.ToString());
                item.SubItems.Add(GetStatusText(save.Status));
                item.Tag = save.SaveName;
                saveListView.Items.Add(item);
            }

            // 默认选择第一个
            if (saveListView.Items.Count > 0)
            {
                saveListView.Items[0].Selected = true;
            }

            // 确定按钮
            var okButton = new Button
            {
                Text = "加载",
                Font = new Font("微软雅黑", 10),
                Size = new Size(80, 30),
                Location = new Point(200, 320),
                BackColor = Color.LightBlue,
                FlatStyle = FlatStyle.Flat,
                DialogResult = DialogResult.OK
            };
            okButton.Click += (sender, e) =>
            {
                if (saveListView.SelectedItems.Count > 0)
                {
                    SelectedSave = saveListView.SelectedItems[0].Tag.ToString();
                }
            };

            // 删除按钮
            var deleteButton = new Button
            {
                Text = "删除",
                Font = new Font("微软雅黑", 10),
                Size = new Size(80, 30),
                Location = new Point(300, 320),
                BackColor = Color.LightCoral,
                FlatStyle = FlatStyle.Flat
            };
            deleteButton.Click += (sender, e) =>
            {
                if (saveListView.SelectedItems.Count > 0)
                {
                    var saveName = saveListView.SelectedItems[0].Tag.ToString();
                    if (MessageBox.Show(string.Format("确定要删除存档 '{0}' 吗？", saveName), "确认删除", 
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (GameDataService.DeleteSave(saveName))
                        {
                            saveListView.SelectedItems[0].Remove();
                            MessageBox.Show("存档删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            };

            // 取消按钮
            var cancelButton = new Button
            {
                Text = "取消",
                Font = new Font("微软雅黑", 10),
                Size = new Size(80, 30),
                Location = new Point(400, 320),
                BackColor = Color.LightGray,
                FlatStyle = FlatStyle.Flat,
                DialogResult = DialogResult.Cancel
            };

            // 添加控件
            this.Controls.AddRange(new Control[]
            {
                titleLabel,
                saveListView,
                okButton,
                deleteButton,
                cancelButton
            });

            // 设置默认按钮
            this.AcceptButton = okButton;
            this.CancelButton = cancelButton;
        }

        /// <summary>
        /// 获取状态文本
        /// </summary>
        private string GetStatusText(GameStatus status)
        {
            switch (status)
            {
                case GameStatus.NotStarted: return "未开始";
                case GameStatus.Playing: return "游戏中";
                case GameStatus.Paused: return "暂停";
                case GameStatus.Won: return "胜利";
                case GameStatus.Lost: return "失败";
                default: return "未知";
            }
        }
    }
} 